import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:shimmer/shimmer.dart';

class AppShimmer extends StatelessWidget {
  final double? width;
  final double? height;
  final BorderRadius? borderRadius;
  final Color? baseColor;
  final Color? highlightColor;

  const AppShimmer({
    Key? key,
    this.width,
    this.height,
    this.borderRadius,
    this.baseColor,
    this.highlightColor,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Shimmer.fromColors(
      baseColor: baseColor ?? Colors.grey[300]!,
      highlightColor: highlightColor ?? Colors.grey[100]!,
      child: Container(
        width: width,
        height: height,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: borderRadius ?? BorderRadius.circular(4.r),
        ),
      ),
    );
  }
}

class AppShimmerList extends StatelessWidget {
  final int itemCount;
  final double? itemHeight;
  final EdgeInsets? padding;
  final double? spacing;

  const AppShimmerList({
    Key? key,
    this.itemCount = 5,
    this.itemHeight,
    this.padding,
    this.spacing,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView.separated(
      padding: padding ?? EdgeInsets.all(16.w),
      itemCount: itemCount,
      separatorBuilder: (context, index) => SizedBox(height: spacing ?? 16.h),
      itemBuilder: (context, index) => AppShimmerListItem(
        height: itemHeight ?? 80.h,
      ),
    );
  }
}

class AppShimmerListItem extends StatelessWidget {
  final double? height;
  final double? avatarSize;
  final bool showAvatar;

  const AppShimmerListItem({
    Key? key,
    this.height,
    this.avatarSize,
    this.showAvatar = true,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: height ?? 80.h,
      padding: EdgeInsets.all(16.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.r),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 4.r,
            offset: Offset(0, 2.h),
          ),
        ],
      ),
      child: Row(
        children: [
          if (showAvatar) ...[
            AppShimmer(
              width: avatarSize ?? 48.w,
              height: avatarSize ?? 48.w,
              borderRadius: BorderRadius.circular((avatarSize ?? 48.w) / 2),
            ),
            SizedBox(width: 16.w),
          ],
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                AppShimmer(
                  width: double.infinity,
                  height: 16.h,
                ),
                SizedBox(height: 8.h),
                AppShimmer(
                  width: 120.w,
                  height: 14.h,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class AppShimmerGrid extends StatelessWidget {
  final int itemCount;
  final int crossAxisCount;
  final double? itemHeight;
  final EdgeInsets? padding;
  final double? spacing;

  const AppShimmerGrid({
    Key? key,
    this.itemCount = 6,
    this.crossAxisCount = 2,
    this.itemHeight,
    this.padding,
    this.spacing,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GridView.count(
      crossAxisCount: crossAxisCount,
      padding: padding ?? EdgeInsets.all(16.w),
      mainAxisSpacing: spacing ?? 16.h,
      crossAxisSpacing: spacing ?? 16.w,
      childAspectRatio: itemHeight != null ? 1.0 : 1.2,
      children: List.generate(
        itemCount,
        (index) => AppShimmerGridItem(height: itemHeight),
      ),
    );
  }
}

class AppShimmerGridItem extends StatelessWidget {
  final double? height;

  const AppShimmerGridItem({
    Key? key,
    this.height,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: height,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.r),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 4.r,
            offset: Offset(0, 2.h),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Expanded(
            flex: 3,
            child: AppShimmer(
              width: double.infinity,
              height: double.infinity,
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(8.r),
                topRight: Radius.circular(8.r),
              ),
            ),
          ),
          Expanded(
            flex: 2,
            child: Padding(
              padding: EdgeInsets.all(12.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  AppShimmer(
                    width: double.infinity,
                    height: 16.h,
                  ),
                  SizedBox(height: 8.h),
                  AppShimmer(
                    width: 80.w,
                    height: 14.h,
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}